package com.lianniu.mgr.cms.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.model.ChildNavigationVo;
import com.lianniu.mgr.cms.service.IChildNavigationService;
import com.lianniu.mgr.pagination.PaginationInfo;

@Controller
@RequestMapping("/rest/childNavigation")
public class ChildNavigationRestController {

private final static Logger logger = Logger.getLogger(ChildNavigationRestController.class);
	
	@Autowired
	private IChildNavigationService ChildNavigationService;
	
	@RequestMapping(value="/ChildNavigationList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult ChildNavigationList(HttpServletRequest request, ChildNavigationVo curVo, PaginationInfo paginationInfo)
	 {
	       logger.info("ChildNavigationRestController  ChildNavigationList begin.");
	       try {
	           
	           logger.info("ChildNavigationList begin.");          
	           BaseResult baseResult = ChildNavigationService.selectByChildNavigationForPage(curVo, paginationInfo);
	           logger.info("ChildNavigationList end.");
	           logger.info("ChildNavigationRestController ChildNavigationList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ChildNavigationRestController ChildNavigationList error:" + e.getMessage());
	       logger.error("ChildNavigationRestController ChildNavigationList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	
	@RequestMapping(value = {"/ChildNavigationAdd"}, method = RequestMethod.POST)
	public @ResponseBody BaseResult ChildNavigationAdd(HttpServletRequest request,ChildNavigationVo curVo) {
	      logger.info("ChildNavigationRestController ChildNavigationAdd begin.");      
	      logger.info("ChildNavigationRestController ChildNavigationAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ChildNavigationAdd begin.");
	        
	          BaseResult baseResult = ChildNavigationService.insertChildNavigation(curVo);
	          logger.info("ChildNavigationAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ChildNavigationAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ChildNavigationAdd error:"+e.getMessage());
	      logger.info("ChildNavigationRestController ChildNavigationAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	
	 
	@RequestMapping(value = {"/ChildNavigationEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ChildNavigationEdit(HttpServletRequest request,ChildNavigationVo curVo) {
	      logger.info("ChildNavigationRestController ChildNavigationEdit begin.");      
	      logger.info("ChildNavigationRestController ChildNavigationEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("ChildNavigationEdit begin.");
	          BaseResult baseResult = ChildNavigationService.updateById(curVo);
	          logger.info("ChildNavigationEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ChildNavigationEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ChildNavigationRestController error:"+e.getMessage());
	      logger.info("ChildNavigationRestController ChildNavigationEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }
	  
	
	 @RequestMapping(value="/selectChildNavigationById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectChildNavigationById(HttpServletRequest request,ChildNavigationVo curVo) {
	        logger.info("ChildNavigationRestController selectChildNavigationById begin.");
	        try {
	                    
	          BaseResult baseResult = ChildNavigationService.selectById(curVo);
	          logger.info("ChildNavigationRestController end.");
	          logger.info("ChildNavigationRestController selectChildNavigationById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ChildNavigationRestController selectChildNavigationById error:" + e.getMessage());
	          logger.error("ChildNavigationRestController selectChildNavigationById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	 
	 @RequestMapping(value = {"/ChildNavigationDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ChildNavigationDel(HttpServletRequest request) {
	      logger.info("ChildNavigationRestController ChildNavigationDel begin.");
	     try {          
	          logger.info("ChildNavigationDel begin.");
	          BaseResult baseResult = ChildNavigationService.deleteChildNavigationBatch(request.getParameter("ids").split(","));		
	          logger.info("ChildNavigationDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ChildNavigationDel end.");
	          
	          logger.info("ChildNavigationRestController ChildNavigationDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ChildNavigationRestController ChildNavigationDel error:" + e.getMessage());
	      logger.info("ChildNavigationRestController ChildNavigationDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	 
	 @RequestMapping(value = {"/checkXName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkXName(HttpServletRequest request) {
	      Boolean flag=true;
	      String xName=  request.getParameter("xName");
	      logger.info("ChildNavigationRestController checkXName begin.");
	     try {          
	          logger.info("checkXName begin.");
	          if("".equalsIgnoreCase(xName)||xName==null)
	          {return flag;}
	          flag = ChildNavigationService.checkXName(xName);         
	          logger.info("checkXName end.");
	          
	          logger.info("ChildNavigationRestController checkXName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ChildNavigationRestController checkXName error:" + e.getMessage());
	      logger.info("ChildNavigationRestController checkXName end.");
	      return flag;
	    }

	  }
	 
	 @RequestMapping(value = {"/checkXNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkXNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String xName=  request.getParameter("xName");
	      String id=request.getParameter("id");
	      logger.info("ChildNavigationRestController checkXNameEdit begin.");
	     try {          
	          logger.info("checkXNameEdit begin.");
	          ChildNavigationVo vo=new ChildNavigationVo();	         
	          vo.setId(id);
	          
	          BaseResult baseResult =ChildNavigationService.selectById(vo);	          
	          ChildNavigationVo curVo=(ChildNavigationVo) baseResult.getData();
	          if(curVo.getId().equals(id)&&curVo.getxName().equals(xName))
	          {return flag;}
	          if("".equalsIgnoreCase(xName)||xName==null)
	          {return flag;}
	          flag = ChildNavigationService.checkXName(xName);         
	          logger.info("checkXNameEdit end.");
	          
	          logger.info("ChildNavigationRestController checkXNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ChildNavigationRestController checkXNameEdit error:" + e.getMessage());
	      logger.info("ChildNavigationRestController checkXNameEdit end.");
	      return flag;
	    }

	  }
}
